<?php
defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_app_class('admin', 'admin', 0);

class manage extends admin {
	private $db;
	
	public $site;
	
	function __construct() {
		parent::__construct();
		pc_base::load_app_class('global');
		$this->db = pc_base::load_model('talent_model');//加载model
		$this->admin_db = pc_base::load_model('admin_model');
		$this->experience_db = pc_base::load_model('experience_model');
		$this->experience_type_db = pc_base::load_model('experience_type_model');
		$this->siteid = get_siteid();//获取当前站点
		$this->admin_userid = param::get_cookie('userid'); // 获取当前登录的管理员ID
	}
	
	public function init() {
		$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
		$where = "";
		$where.= "is_signed_model = 1 and status != 2 and siteid=".$this->siteid;
		if ($_GET['keyword'] || $_GET['address']) {
			if (!empty($_GET['keyword'])) {
				if ($_GET['key'] == 1) {
					$keyword = $_GET['keyword'];
					$where.= " and real_name = '$keyword'";
				} elseif ($_GET['key'] == 2) {
					$keyword = $_GET['keyword'];
					$where.= " and telephone = '$keyword'";
				} elseif ($_GET['key'] == 3) {
					$keyword = $_GET['keyword'];
					$where.= " and identification = '$keyword'";
				}
			}
			if ($_GET['address']) {
				$address = $_GET['address'];
				$where.=" and address LIKE '%$address%'";
			}
			$moter_info = $this->db->listinfo($where,'follow DESC,praise DESC',$page,10);//人才搜索按照点赞人数、关注人数由高到低排列
		} else {
			$moter_info = $this->db->listinfo($where,'id DESC',$page,10);
		}
		$username = array();
		foreach ($moter_info as $k=>$v){
			$username = $this->admin_db->get_one(array('userid'=>$v['updated_by']),'username');
			$moter_info[$k]['username'] = $username['username'];
		}
		$pages = $this->db->pages;
		pc_base::load_sys_class('format', '', 0);
		include $this->admin_tpl('moter_list');
	}
	
	public function add() {
		if (isset($_POST['dosubmit'])) {
			$result=$this->db->moter_add($_POST['moter']);
			if ($result) {
				showmessage(L('add_success'), '?m=moter&c=manage&a=init');
			} else {
				showmessage(L('add_moter_error'),HTTP_REFERER);
			}

		} else {
			pc_base::load_sys_class('form','',0);
			include $this->admin_tpl('moter_add');
		}
	}
	
	public function edit(){
		$id = isset($_GET['id']) ? intval($_GET['id']) : showmessage(L('message not exits'),HTTP_REFERER);
		$info = $this->db->get_one(array('id'=>$id));
		if (isset($_POST['dosubmit'])) {
			$up_info = isset($_POST['moter']) ? $_POST['moter'] : showmessage(L('illegal_parameters'),HTTP_REFERER);
			$condition = "";
			$condition .= "is_signed_model = 1"." and id != ".$id." and identification = '".$up_info['identification']."'";
			$exist = $this->db->get_one($condition);
			if ($exist) {
				showmessage(L('moter_exist'),HTTP_REFERER);
			}
			$data['real_name'] = $up_info['real_name'];
			$data['en_name'] = $up_info['en_name'];
			$data['nickname'] = $up_info['nickname'];
			$data['gender'] = $up_info['gender'];
			$data['age'] = $up_info['age'];
			$data['birthday'] = strtotime($up_info['birthday']);
			$data['height'] = $up_info['height'];
			$data['weight'] = $up_info['weight'];
			$data['university'] = $up_info['university'];
			$data['diploma'] = $up_info['diploma'];
			$data['province'] = $up_info['province'];
			$data['city'] = $up_info['city'];
			$data['bust'] = $up_info['bust'];
			$data['waist'] = $up_info['waist'];
			$data['hips'] = $up_info['hips'];
			$data['shoe_size'] = $up_info['shoe_size'];
			$data['telephone'] = $up_info['telephone'];
			$data['email'] = $up_info['email'];
			$data['identification'] = $up_info['identification'];
			$data['address'] = $up_info['address'];
			$data['award'] = $up_info['award'];
			$data['hobby'] = $up_info['hobby'];
			$data['introduction'] = $up_info['introduction'];
			$data['is_signed_model'] = $up_info['is_signed_model'];
			$data['image_horizontal_big'] = $up_info['image_horizontal_big'];
			$data['image_vertical_big'] = $up_info['image_vertical_big'];
			$data['image_horizontal_small'] = $up_info['image_horizontal_small'];
			$data['image_vertical_small'] = $up_info['image_vertical_small'];
			$data['image_square'] = $up_info['image_square'];
			$data['update_time'] = time();
			$data['updated_by'] = $this->admin_userid;
			$data['siteid'] = $this->siteid;
			$data['source'] = $up_info['source'];
			$result = $this->db->update($data,array('id'=>$id));
			if ($result) {
				showmessage(L('edit_moter_success'), '?m=moter&c=manage&a=init');
			} else {
				showmessage(L('edit_moter_error'),HTTP_REFERER);
			}
		} else {
			$show_header = false;
			pc_base::load_sys_class('form', '', 0);
			include $this->admin_tpl('moter_edit');
		}
	}
	
	public function del(){
		if (! isset($_REQUEST['id']) || empty($_REQUEST['id'])) {
	            showmessage(L('message not exits'), HTTP_REFERER);
	        }
	        $id = $_REQUEST['id'];
	        if (is_array($id)) {
	            foreach ($id as $k => $v) {
	                if (intval($v)) {
	                    $id[$k] = intval($v);
	                } else {
	                    unset($id[$k]);
	                }
	            }
	            $id = implode('\',\'', $id);
	        }
	        $result = $this->db->update(array(
	            'status' => '2'
	        ), "id in ('$id')");
	        if ($result) {
	            showmessage(L('del_moter_success'), HTTP_REFERER);
	        } else {
	            showmessage(L('del_moter_error'), HTTP_REFERER);
	        }
	}
	
	public function detail() {
		$id = isset($_GET['id']) ? intval($_GET['id']) : showmessage(L('message not exits'),HTTP_REFERER);
		$info = $this->db->get_one(array('id'=>$id));
		$show_header = false;
		pc_base::load_sys_class('form', '', 0);
		include $this->admin_tpl('moter_detail');
	}
	
	/**
	* 添加模特校验身份证号，防止模特重复
	* @date: 2017年3月15日 上午9:57:24
	* @author: LIYANG
	*/
	public function check_moter() {
		$moter_id = $_GET['identification'];
		if (isset($moter_id)){
			$id = $this->db->get_one(array('identification'=>$moter_id,'is_moter'=>1,'siteid'=>$this->siteid));
			if ($id['identification'] == $moter_id) {
				exit('0');
			} else {
				exit('1');
			}
		} else {
			exit('0');
		}
	}
}